package com.cloud.controller;

import com.cloud.model.T_base;
import com.cloud.model.T_result;
import com.cloud.service.BaseService;
import com.cloud.service.ResultService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * @Author wangjian (jian.wang@bertelsmann.com.cn)
 * @Date 2017/2/8 16:17
 */
@RestController
@RequestMapping("/v1/result")
public class ResultController {

    @Autowired
    BaseService baseService;

    @Autowired
    ResultService resultService;


    @ApiOperation(value="首月数据导入结果表",notes="爱华")
    @RequestMapping(value="/insertResult",method= RequestMethod.POST)
    public void insertResult(){

        // 组装数据
        List<T_base> bList = baseService.findTotal();
        String username="";
        String fmonth="";
        int i=0;
        for(T_base b:bList){
            i++;
            if("".equals(username)){
                username = b.getUsername();
            }
            int unum = Integer.parseInt(b.getUnum());
            if(username.equals(b.getUsername())&&unum>0){
                fmonth=fmonth+b.getUname()+"/";
            }
            if(!username.equals(b.getUsername())){
                T_result r = new T_result();
                r.setUsername(username);
                r.setFmonth(fmonth.substring(0, fmonth.length() - 1));
                resultService.insert(r);
                fmonth="";
                username=b.getUsername();
                fmonth=fmonth+b.getUname()+"/";
            }
            if(i==bList.size()){
                T_result r = new T_result();
                r.setUsername(username);
                r.setFmonth(fmonth.substring(0,fmonth.length()-1));
                resultService.insert(r);
            }
        }
    }

    @ApiOperation(value="次月数据导入结果表并且对比",notes="爱华")
    @RequestMapping(value="/updateResult",method= RequestMethod.POST)
    public void updateResult(){

        // 组装数据
        List<T_base> bList = baseService.findTotal();
        String username="";
        String fmonth="";
        int flag=0;
        for(T_base b:bList){
            flag++;
            if("".equals(username)){
                username = b.getUsername();
            }
            int unum = Integer.parseInt(b.getUnum());
            if(username.equals(b.getUsername())&&unum>0){
                fmonth=fmonth+b.getUname()+"/";
            }
            if(!username.equals(b.getUsername())){
                T_result r = new T_result();
                r.setUsername(username);
               List<T_result> rList =  resultService.findAll(r);
                if(rList.size()>0){
                    T_result re = new T_result();
                    re.setId(rList.get(0).getId());
                    re.setSmonth(fmonth.substring(0, fmonth.length() - 1));
                    resultService.updateResult(re);
                }else{
                    T_result r1 = new T_result();
                    r1.setUsername(username);
                    r1.setSmonth(fmonth.substring(0,fmonth.length()-1));
                    resultService.insert(r1);
                }
                fmonth="";
                username=b.getUsername();
                fmonth=fmonth+b.getUname()+"/";
            }
            if(flag==bList.size()){
                T_result r = new T_result();
                r.setUsername(username);
                List<T_result> rList =  resultService.findAll(r);
                if(rList.size()>0){
                    T_result re = new T_result();
                    re.setId(rList.get(0).getId());
                    re.setSmonth(fmonth.substring(0, fmonth.length() - 1));
                    resultService.updateResult(re);
                }else{
                    T_result r1 = new T_result();
                    r1.setUsername(username);
                    r1.setSmonth(fmonth.substring(0,fmonth.length()-1));
                    resultService.insert(r1);
                }
            }
        }

        T_result result = new T_result();
        List<T_result> tList = resultService.findAll(result);
        for(T_result t:tList){
            if(t.getFmonth()==null&&t.getSmonth()!=null){
                t.setMore(t.getSmonth());
                t.setMorenum(String.valueOf(t.getSmonth().split("/").length));
                resultService.updateResult(t);
                continue;
            }else if(t.getFmonth()!=null&&t.getSmonth()==null){
                t.setLess(t.getFmonth());
                t.setLessnum(String.valueOf(t.getFmonth().split("/").length));
                resultService.updateResult(t);
                continue;
            }
            String b[] = t.getFmonth().split("/");
            String c[] = t.getSmonth().split("/");
            String d="";
            String e="";
            if(t.getFmonth().equals(t.getSmonth())){
                continue;
            }
            for(int i=0;i<b.length;i++ ){
                if(!Arrays.asList(c).contains(b[i])){
                    d=d+b[i]+"/";
                }
            }

            for(int j=0;j<c.length;j++ ){
                if(!Arrays.asList(b).contains(c[j])){
                    e=e+c[j]+"/";
                }
            }
            if(!"".equals(d)||!"".equals(e)){

                if(!"".equals(e)){
                    t.setMore(e.substring(0, e.length() - 1));
                    t.setMorenum(String.valueOf(e.substring(0, e.length() - 1).split("/").length));
                }

                if(!"".equals(d)){
                    t.setLess(d.substring(0, d.length() - 1));
                    t.setLessnum(String.valueOf(d.substring(0, d.length() - 1).split("/").length));
                }
                resultService.updateResult(t);
            }

        }
    }
}
